<template>
  <div class="login-account">
    <el-form :rules="rules" :model="account" ref="formRef">
      <!-- label-width表示label的宽度 -->
      <!-- props是方便rules规则匹配的 -->
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" showPassword></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { ElForm } from "element-plus";
import { defineComponent, reactive, ref } from "vue";
import { rules } from "../config/account-config";
import localCache from "@/utils/cache";
import { useStore } from "vuex";
export default defineComponent({
  setup() {
    const store = useStore();
    const account = reactive({
      name: localCache.getCache("name") || "",
      password: localCache.getCache("password") || "",
    });
    const formRef = ref<InstanceType<typeof ElForm>>();
    const loginAction = (iskeepPassword: boolean) => {
      formRef.value?.validate((valid: boolean) => {
        //判断输入的账号密码是否通过校验
        if (valid) {
          console.log("真正的登录逻辑");
          //1.判断是否需要记住密码
          if (iskeepPassword) {
            //本地缓存
            localCache.setCache("name", account.name);
            localCache.setCache("password", account.password);
          } else {
            localCache.deleteCache("name");
            localCache.deleteCache("password");
          }
          //2.进行登录验证
          store.dispatch("login/accountLoginAction", { ...account });
        }
      });
    };
    return {
      account,
      rules,
      loginAction,
      formRef,
    };
  },
});
</script>

<style scoped></style>
